package com.zb.controller;


import cn.dev33.satoken.stp.StpUtil;
import com.zb.entity.AdmInfo;
import com.zb.entity.dto.AccountDto;
import com.zb.entity.dto.AccountInfoDto;
import com.zb.entity.dto.PasswordDto;
import com.zb.enums.TokenType;
import com.zb.exception.LoginException;
import com.zb.exception.RegisterException;
import com.zb.exception.SMSException;
import com.zb.redis.RedisLoginTokenUtil;
import com.zb.result.Result;
import com.zb.service.ZjAdmAccountService;
import com.zb.validation.AddValidation;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * <p>
 * 后台账号表 前端控制器
 * </p>
 *
 * @author WEN
 * @since 2023-07-07
 */
@Api(tags = "账号类接口")
@RestController
@RequestMapping("/account")
public class ZjAdmAccountController {
    @Autowired
    private ZjAdmAccountService admAccountService;

    @ApiOperation(value = "注册账号接口")
    @PostMapping("/registerAccount")
    public Result registerAccount(@RequestBody @Validated(value = {AddValidation.class}) AccountDto accountDto) throws RegisterException {
        admAccountService.registerAccount(accountDto);
        return Result.success("账号注册成功！");
    }

    @ApiOperation(value = "获取登录信息")
    @GetMapping("/getInfo")
    public Result getInfo(HttpServletRequest request) {
        String accessToken = request.getHeader(TokenType.ACCESS_TOKEN.getType());
        AdmInfo admInfo = RedisLoginTokenUtil.getAdmInfoByAccessToken(accessToken);
        return Result.success("获取成功").data("row", admAccountService.getInfo(admInfo.getId()));
    }

    @ApiOperation(value = "登录界面更改登录密码")
    @PostMapping("/loginPageUpdatePassword")
    public Result loginPageUpdatePassword(@RequestBody @Valid PasswordDto passwordDto) throws LoginException, SMSException {
        admAccountService.updatePassword(passwordDto);
        return Result.success("密码更改成功！");
    }

    @ApiOperation(value = "更改登录密码")
    @PostMapping("/updatePassword")
    public Result updatePassword(HttpServletRequest request, @RequestBody @Valid PasswordDto passwordDto) throws LoginException, SMSException {
        admAccountService.updatePassword(passwordDto);
        return Result.success("密码更改成功！");
    }

    @ApiOperation(value = "更改账号信息")
    @PostMapping("/updateAccountInfo")
    public Result updateAccountInfo(HttpServletRequest request, @RequestBody AccountInfoDto accountInfoDto) {
        String accessToken = request.getHeader(TokenType.ACCESS_TOKEN.getType());
        Long loginId = (Long) StpUtil.getLoginIdByToken(accessToken);
        admAccountService.updateAccountInfo(loginId, accountInfoDto);
        return Result.success("更改账号信息成功！");
    }

    @ApiOperation(value = "更换手机号")
    @GetMapping("/updatePhone")
    public Result updatePhone(HttpServletRequest request,
                              @ApiParam(value = "新手机号", required = true) String newPhone,
                              @ApiParam(value = "验证码", required = true) String code) throws RegisterException, SMSException {
        String accessToken = request.getHeader(TokenType.ACCESS_TOKEN.getType());
        AdmInfo admInfo = RedisLoginTokenUtil.getAdmInfoByAccessToken(accessToken);
        admAccountService.updatePhone(admInfo, newPhone, code);
        RedisLoginTokenUtil.setAdmInfoByAccessToken(accessToken, admInfo);
        return Result.success("更换手机号成功！");
    }

    @ApiOperation(value = "更换登录账号")
    @GetMapping("/updateAccount")
    public Result updateAccount(HttpServletRequest request, @ApiParam(value = "登录账号", required = true) String account,
                                @ApiParam(value = "验证码", required = true) String code) throws RegisterException, SMSException {
        if (StringUtils.isBlank(account)) {
            throw new IllegalArgumentException("登录账号不能为空");
        }
        if (account.length() > 50) {
            throw new IllegalArgumentException("登录账号字符长度限制在50个以内");
        }
        String accessToken = request.getHeader(TokenType.ACCESS_TOKEN.getType());
        AdmInfo admInfo = RedisLoginTokenUtil.getAdmInfoByAccessToken(accessToken);
        admAccountService.updateAccount(admInfo, account, code);
        RedisLoginTokenUtil.setAdmInfoByAccessToken(accessToken, admInfo);
        return Result.success("更换登录账号成功！");
    }






}
